package graph.group.zhangtao.guider.getter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

import graph.group.zhangtao.guider.utils.GetLocalFiles;

/**
 * Created by tao on 18-3-19.
 */

public class ImageGetterFactory extends AsyncTask {
    private ImageView imageView;
    private String path;

    public ImageGetterFactory(ImageView imageView, String path) {
        this.imageView = imageView;
        this.path = path;
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                BitmapFactory.Options opt = new BitmapFactory.Options();
                //压缩编码
                opt.inPreferredConfig = Bitmap.Config.RGB_565;
                //下面两个过时了，但没影响
                opt.inPurgeable = true;
                opt.inInputShareable = true;

                return BitmapFactory.decodeStream(inputStream, null, opt);
            }
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        if (o != null) {
            imageView.setImageBitmap((Bitmap) o);
        }
    }

//    public static void setImageView(ImageView imageView, String path, String id, String extendPath, boolean fromfile) {
//        if (fromfile) {
//            path = path + id + "/" + extendPath;
//        } else {
//            try {
//                String tempId = URLEncoder.encode(id, "UTF-8");
//                path = path + tempId + "/" + extendPath;
//                new ImageGetterFactory(imageView, path).execute();
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//        }
//    }

    public static void setImageView(ImageView imageView, String path, boolean fromfile) {
        if (fromfile) {
            BitmapFactory.Options opt = new BitmapFactory.Options();
            //压缩编码
            opt.inPreferredConfig = Bitmap.Config.RGB_565;
            //下面两个过时了，但没影响
            opt.inPurgeable = true;
            opt.inInputShareable = true;
            InputStream inputStream = GetLocalFiles.GetFromFile(path);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, opt);
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageView.setImageBitmap((Bitmap) bitmap);
        } else {
            new ImageGetterFactory(imageView, path).execute();
        }
    }
}
